package liumengbaio.lolzhangyoubao.Activity.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;

import liumengbaio.lolzhangyoubao.Activity.Adapter.MoreTreasureRecordsAdapter;
import liumengbaio.lolzhangyoubao.Activity.InFo.MoreTreasureRecordInfo;
import liumengbaio.lolzhangyoubao.Activity.utils.HttpUtils;
import liumengbaio.lolzhangyoubao.R;

/**
 * 往期记录界面
 * Created by lanouhn on 16/9/12.
 */
public class MoreTreasureRecordActivity extends AppCompatActivity {
    private String id;
    private ImageView back;
    private ListView mListView;
    private MoreTreasureRecordInfo mtri;
    private MoreTreasureRecordsAdapter mtra;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.more_treasure_record);
        initView();
        initData();
        initEvent();
        getRecordData();
    }

    private void initView() {
        mListView = (ListView) findViewById(R.id.more_record_listView);
        back = (ImageView) findViewById(R.id.more_treasure_back);
    }

    private void initData() {
        Intent intent = getIntent();
        id = intent.getStringExtra("record_id");
    }

    private void initEvent() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void getRecordData() {
        String url = "http://lol.bet.zhangyoubao.com/service/rest?api=raid.raidgoodshistory&game=lol&platformVersion=400901";
        String urlBody = "apiVersion=v1&platform=android&os=android&osVersion=19&params%5Braid_define_id%5D=" + id + "&secretSign" +
                "ature=uyiCc1srmE8n8iNVVnQ7pmGDcYA%3D&game=lol&sign=&platformVersion=400901&time=1473663059&secretId=AKIDz8k" +
                "rbsJ5yKBZQpn74WFkmLPx3gnPhESA&nonce=792439&userToken=1285c08b612411d0bfc35f213eb5d92a73&secretVersion=v1.0&" +
                "userId=15863305&api=raid.raidgoodshistory&deviceId=JfoZvarCyLfJIqs9SRFolapk21%2FwEw63JVMaX7Y3v5w%3D&params%5Bsort%5D=0&";
        try {
            HttpUtils.doPostAsyn(url, urlBody, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson = new Gson();
                    mtri = gson.fromJson(result, MoreTreasureRecordInfo.class);
                }
            }, handler);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    mtra = new MoreTreasureRecordsAdapter(MoreTreasureRecordActivity.this, mtri);
                    mListView.setAdapter(mtra);
                    break;
                case 1:
                    Toast.makeText(MoreTreasureRecordActivity.this, "数据接受失败！", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
}
